
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_book/api/search/search_api.dart';
import 'package:flutter_book/constant/color.dart';
import 'package:flutter_book/page/search/search_history_text_page.dart';
import 'package:flutter_book/page/search/search_key_words_page.dart';
import 'package:flutter_book/page/search/search_result_page.dart';
import 'package:flutter_book/util/extensions/string_extensions.dart';
import 'package:flutter_book/util/extensions/widget_extensions.dart';
import 'package:get/get.dart';

///搜索页
class SearchBookPage extends StatelessWidget {
  final SearchTextController _searchTextController=SearchTextController();
  final FocusNode _focusNode=FocusNode();
  final TextEditingController _controller=TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: _getAppBar(context),
        body: Obx(()=>_getBody()),
    ).onTap((){
      FocusScope.of(context).requestFocus(FocusNode());
    });
  }

  //判断展示什么ui
  Widget _getBody(){
   if(_searchTextController.searchText.value.length<1){
     //获取历史记录
     return SearchHistoryTextPage(_onClickTextListener);
   }else if(_focusNode.hasFocus){
     //获取预搜索标签
     return SearchKeyWordsPage(_searchTextController.searchText.value);
   }else{
     //获取搜索结果页
     return SearchResultPage(_searchTextController.searchText.value);
   }
  }


  //获取appbar
  PreferredSizeWidget _getAppBar(BuildContext context){
    return  AppBar(
      elevation: 0,
      automaticallyImplyLeading: false,
      titleSpacing: 0.0,
      title: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          InkWell(
              child: Container(
                padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
                child: Icon(Icons.arrow_back),
              ),
              onTap: () {
                Navigator.of(context).pop();
              }),
          Expanded(
            flex: 1,
            child: Padding(
              padding: EdgeInsets.only(right: 40),
              child: Container(
                  height: 40,
                  decoration: BoxDecoration(
                      borderRadius:
                      BorderRadius.all(Radius.circular(20)),
                      color: Color(0xFFF5F5F5)),
                  alignment: Alignment.center,
                  child: TextField(
                    keyboardType: TextInputType.text,
                    focusNode: _focusNode,
                    controller: _controller,
                    textInputAction: TextInputAction.search,
                    textAlignVertical: TextAlignVertical.center,
                    onChanged: (data) {
                      _searchTextController.setSearchText(data);
                      // inputStreamController.sink.add(data);
                    },
                    style: TextStyle(
                        textBaseline: TextBaseline.alphabetic,
                        fontSize: 15),
                    decoration: InputDecoration(
                        hintText: "在此输入你要查询的书籍名",
                        // 提示文本样式
                        hintStyle: TextStyle(fontSize: 14, color: AppColor.textHint),
                        prefixIcon: Icon(Icons.search),
                        border: InputBorder.none),
                  )),
            ),
          )
        ],
      ),
    );
  }




  //开始搜索某一个字符串
  _onClickTextListener(String searchText){
    searchText.print;
    _controller.text=searchText;
    var setCursor = TextSelection(
      baseOffset: searchText.length,
      extentOffset: searchText.length,
    );
    _controller.selection = setCursor;
    _searchTextController.setSearchText(searchText);
    // SearchApi.init().searchTargetKeyWord(searchText);
  }


}


class SearchTextController extends GetxController{
  var searchText="".obs;

  setSearchText(String text)=>searchText.value=text;
}
